<?php
namespace Comment\Controller;

use Common\Controller\AdminbaseController;
use Common\Model\CommentsModel;

class CommentadminController extends AdminbaseController{
	
	protected $comments_model;
	
	public function _initialize(){
		parent::_initialize();
		$this->comments_model=D("Common/Comments");
	}

	// 后台评论列表
	public function index($table=""){
		$where = array('type' => CommentsModel::COMMENT_TYPE_USER_COMMENT);
		if(!empty($table)){
			$where['post_table']=$table;
		}
		
		$post_id=I("get.post_id");
		if(!empty($post_id)){
			$where['post_id']=$post_id;
		}
		$count=$this->comments_model->where($where)->count();
		$page = $this->page($count, 20);
		$comments=$this->comments_model
		->where($where)
		->limit($page->firstRow . ',' . $page->listRows)
		->order("createtime DESC")
		->select();
		$this->assign("comments",$comments);	
		$this->assign("page", $page->show('Admin'));
		$this->display(":index");
	}
	
	// 后台评论删除
	public function delete(){
		if(isset($_GET['id'])){
			$id = intval(I("get.id"));
			if ($this->comments_model->where("id = $id")->delete()!==false) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
		if(isset($_POST['ids'])){
			$ids=join(",",$_POST['ids']);
			if ($this->comments_model->where("id in ($ids)")->delete()!==false) {
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}
		}
	}
	
	// 后台评论审核
	public function check(){
		if(isset($_POST['ids']) && $_GET["check"]){
			$data["status"]=1;
				
			$ids=join(",",$_POST['ids']);
			
			if ( $this->comments_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("审核成功！");
			} else {
				$this->error("审核失败！");
			}
		}
		if(isset($_POST['ids']) && $_GET["uncheck"]){
				
			$data["status"]=0;
			$ids=join(",",$_POST['ids']);
			if ( $this->comments_model->where("id in ($ids)")->save($data)!==false) {
				$this->success("取消审核成功！");
			} else {
				$this->error("取消审核失败！");
			}
		}
	}

	// 文章评审信息列表
    public function judge_list()
    {
        $post_id        = I('get.post_id', 0, 'intval');
        $comment_model  = D("Common/Comments");
        $where          = array("post_table" => 'posts', "post_id" => $post_id, 'type' => CommentsModel::COMMENT_TYPE_JUDGE_INFO);
        $count          = $comment_model->where($where)->count();
        $page           = $this->page($count, 20);
        $judge_lists    = $comment_model->where($where)->limit($page->firstRow . ',' . $page->listRows)->order("createtime desc")->select();
        $judge_lists    = array_map(function ($v) {
            $v['content'] = str_replace('||', '<br>', $v['content']);
            return $v;
        }, $judge_lists);
        $this->assign('judge_list', $judge_lists);
        $this->assign("page", $page->show('Admin'));
        $this->assign('post_id', $post_id);
        $this->display();
	}

	// 添加文章审核信息
    public function edit_judge()
    {
        $comment_id     = I('get.comment_id', 0, 'intval');
        $post_id        = I('get.post_id', 0, 'intval');
        $comment_model  = D("Common/Comments");
        $admin_user     = sp_get_current_admin_user();

        $this->assign('post_id', $post_id);
        if ($comment_id) {
            if (I('get.del', 0, 'intval')) {
                $comment_model->where(['id' => $comment_id])->save(['status' => CommentsModel::COMMENT_STATUS_DEL]);
                $this->success('删除成功');
            }

            $judge_info = $comment_model->where(['id' => $comment_id])->find();
            $this->assign('comment_id', $comment_id);
        }

        if (IS_POST) {
            if ($judge_info) {
                $data['status']     = I('post.status');
                $data['content']    = I('post.content');
                $comment_model->where(['id' => $comment_id])->save($data);
                $msg = '修改成功';
            } else {
                $data = array(
                    'post_table'        => 'posts',
                    'post_id'           => $post_id,
                    'uid'               => $admin_user['id'],
                    'full_name'         => $admin_user['user_nicename'],
                    'createtime'        => date("Y-m-d H:i:s", time()),
                    'content'           => I('post.content'),
                    'type'              => CommentsModel::COMMENT_TYPE_JUDGE_INFO,
                    'status'            => CommentsModel::COMMENT_STATUS_NORMAL,
                );
                $comment_model->add($data);
                $msg = "添加成功";
            }
            $this->success($msg, U('Commentadmin/judge_list', array('post_id' => $post_id)));

        } else {

            if ($judge_info) {
                $content = $judge_info['content'];
                list($reason, $suggest) = explode('||', $content);
                !empty($reason) && $judge_info['reason'] = $reason;
                !empty($suggest) && $judge_info['suggest'] = $suggest;

                $this->assign('judge_info', $judge_info);
            }
            $this->display();
        }
	}

    // 问答评论列表
    public function question_comment_list()
    {
        $post_id        = I('get.post_id', 0, 'intval');
        $comment_model  = D("Common/Comments");
        $where          = array("post_table" => 'posts', "post_id" => $post_id, 'type' => CommentsModel::COMMENT_TYPE_USER_COMMENT);
        $count          = $comment_model->where($where)->count();
        $page           = $this->page($count, 20);
        $comment_lists  = $comment_model->where($where)->limit($page->firstRow . ',' . $page->listRows)->order("createtime desc")->select();
        $this->assign('comment_list', $comment_lists);
        $this->assign("page", $page->show('Admin'));
        $this->assign('post_id', $post_id);
        $this->display();
	}
	
}